import Group from 'ol/layer/group';
import Tile from 'ol/layer/tile';
import OSM from 'ol/source/osm';
import XYZ from 'ol/source/xyz';
import BingMaps from 'ol/source/bingmaps';

export function openstreet(): any {
    return new Group({
        zIndex: 1,
        layers: [
            new Tile({
                source: new OSM()
            })
        ]
    });
}

export function tiandituRoad(): any {
    return new Group({
        zIndex: 1,
        layers: [
            new Tile({
                source: new XYZ({
                    url: 'http://t3.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}'
                })
            }),
            new Tile({
                source: new XYZ({
                    url: 'http://t3.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}'
                })
            })
        ]
    });
}

export function tiandituSatellite(): any {
    return new Group({
        zIndex: 1,
        layers: [
            new Tile({
                source: new XYZ({
                    url: 'http://t3.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}'
                })
            }),
            new Tile({
                source: new XYZ({
                    url: 'http://t3.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}'
                })
            })
        ]
    });
}

export function tiandituTerrain(): any {
    return new Group({
        zIndex: 1,
        layers: [
            new Tile({
                source: new XYZ({
                    url: 'http://t3.tianditu.com/DataServer?T=ter_w&x={x}&y={y}&l={z}'
                })
            }),
            new Tile({
                source: new XYZ({
                    url: 'http://t3.tianditu.com/DataServer?T=cta_w&x={x}&y={y}&l={z}'
                })
            })
        ]
    });
}

export function bingmapSatellite(): any {
    let key: string = this.bingKey;
    return new Group({
        zIndex: 1,
        layers: [
            new Tile({
                source: new BingMaps({
                    key,
                    imagerySet: 'AerialWithLabels'
                })
            })
        ]
    });
}

export function bingmapRoad(): any {
    let key: string = this.bingKey;
    return new Group({
        zIndex: 1,
        layers: [
            new Tile({
                source: new BingMaps({
                    key,
                    imagerySet: 'Road'
                })
            })
        ]
    });
}

export function xyz(): any {
    let urls: string[] = this.url;
    let layers: any[] = urls.reduce((state: any[], url: string) => {
        return [...state, new Tile({
            source: new XYZ({
                url: url
            })
        })];
    }, []);
    return new Group({
        zIndex: 1,
        layers
    });
}

export const funs = {
    OPENSTREETMAP: openstreet,
    TIANDITU_ROAD: tiandituRoad,
    TIANDITU_SATELLITE: tiandituSatellite,
    TIANDITU_TERRAIN: tiandituTerrain,
    BINGMAP_SATELLITE: bingmapSatellite,
    BINGMAP_ROAD: bingmapRoad,
    XYZ: xyz
};